(function ($) {
    "use strict";

    /**
     * 页面缓存
     * @type {{}}
     */
    top.window.cacheMap = top.window.cacheMap || {};

    var kirin = {};

    /**
     * 弹出窗口
     * @type {{open: Function, done: Function, notify: Function, fail: Function}}
     */
    kirin.popup = {
        open     : function (options) {
            // 默认参数
            var defaults = {
                type      : 2,
                title     : false,
                shadeClose: false,
                shade     : [0.5, '#000'],
                maxmin    : true,
                scrollbar : false
            };

            // 覆盖插件默认参数
            var opts = $.extend({}, defaults, options);

            // 设置宽高，为空则用默认
            var width = (opts.width || 800), height = (opts.height || ($(window).height() - 50));
            opts.area = [width + 'px', height + 'px'];

            // 使用promise的方式
            var defer = $.Deferred();

            // 弹出layer
            var i = top.layer.open(opts);

            top.window.cacheMap['kirin.popup.open.' + i] = defer;

            //子页面的共享数据
            top.window.cacheMap['kirin.popup.data.' + i] = opts.shareData;

            return defer.promise();
        },
        shareData: function () {
            var i   = top.layer.getFrameIndex(window.name);
            var key = 'kirin.popup.data.' + i;
            return top.window.cacheMap[key];
        },
        success  : function (data) {
            var i     = top.layer.getFrameIndex(window.name);
            var key   = 'kirin.popup.open.' + i;
            var defer = top.window.cacheMap[key];
            defer.resolve(data);
            top.window.layer.close(i);
        },
        notify   : function (data) {
            var i     = top.layer.getFrameIndex(window.name);
            var key   = 'kirin.popup.open.' + i;
            var defer = top.window.cacheMap[key];
            defer.notify(data);
        },
        close    : function (data) {
            var i     = top.layer.getFrameIndex(window.name);
            var key   = 'kirin.popup.open.' + i;
            var defer = top.window.cacheMap[key];

            delete top.window.cacheMap[key];
            delete top.window.cacheMap['kirin.popup.data.' + i];

            defer.reject(data);
            top.window.layer.close(i);
        }

    };

    /**
     * 初始化开始结束时间选择框的设置
     * PS:
     * {
     *      start : 选择器
     *      end   : 选择器
     *      format: 'YYYY-MM-DD hh:mm:ss' --默认
     *      min   : 最小日期
     *      max   : 最大日期
     *      istoday :false
     *      event : 'focus'
     *      startChooseCallback: callback
     *      endChooseCallback : callback
     *  }
     * @param options {}
     */
    kirin.initRangeDateTime = function (options) {

        //日期控件
        if (!window.laydate) {
            alert("请安装[laydate]日期控件。");
        }

        //默认参数
        var defaults = {
            format             : 'YYYY-MM-DD hh:mm:ss',
            min                : '2000-01-01',
            max                : laydate.now(),
            istoday            : false,
            event              : 'focus',
            startChooseCallback: function (start, end, chooseDate) {
                end.min   = chooseDate;
                end.start = chooseDate;
            },
            endChooseCallback  : function (start, end, chooseDate) {
                //start.min = chooseDate;
                //start.start = chooseDate;
            }
        };

        //使用jQuery.extend 覆盖默认参数
        var opts = $.extend({}, defaults, options);

        //开始
        var start = {
            elem   : opts.start,
            format : opts.format,
            max    : opts.max,
            istoday: opts.istoday,
            choose : function (chooseDate) {
                opts.startChooseCallback && opts.startChooseCallback(start, end, chooseDate);
            },
            event  : opts.event
        };

        //结束
        var end = {
            elem   : opts.end,
            format : opts.format,
            min    : opts.min,
            istoday: opts.istoday,
            choose : function (chooseDate) {
                opts.endChooseCallback && opts.endChooseCallback(start, end, chooseDate);
            },
            event  : opts.event
        };

        laydate(start);
        laydate(end);
    };

    /**
     * 时间格式化
     * @param timestamp
     * @param format
     * @returns {*}
     */
    kirin.dataFormat = function (timestamp, format) {
        return moment(timestamp).format(format);
    };


    // 暴露为全局变量
    window.kirin = kirin;
})(window.jQuery);